package com.google.code.swingonfire.component.calendar;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.TableCellRenderer;

import com.google.code.swingonfire.component.calendar.JMonthTableModel.WeekOfYearEntry;

public class JMonthTableWeakOfYearRenderer implements TableCellRenderer
{

    private final JLabel label;
    private final Font defaultFont;
    private final Color defaultForeground;

    public JMonthTableWeakOfYearRenderer()
    {
        super();

        label = new JLabel();

        prepare(label);

        defaultFont = label.getFont();
        defaultForeground = label.getForeground();
    }

    protected void prepare(JLabel label)
    {
        label.setHorizontalAlignment(SwingConstants.CENTER);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column)
    {
        WeekOfYearEntry entry = (WeekOfYearEntry) value;

        label.setText(entry.getWeekOfYearName());

        Font font = entry.getWeekOfYearFont(defaultFont);

        label.setFont((font != null) ? font : defaultFont);

        Color foreground = entry.getWeekOfYearForeground(defaultForeground);

        label.setForeground((foreground != null) ? foreground : defaultForeground);

        return label;
    }

}
